<?php

namespace app\admin\controller\auth;

use app\admin\controller\Base;
use brand\Brand;
use brand\build\Columns;
use brand\page\Form;
use brand\page\Table;
use brand\tools\Tree;
use app\admin\model\AuthRule;

/**
 * 规则管理.
 *
 */
class Rule extends Base
{
    /**
     * @var \app\admin\model\AuthRule
     */
    protected $model = null;
    protected $rulelist = [];
    protected $curdName = 'auth_rule';
    protected $pk = 'id';


    public function _initialize()
    {
        parent::_initialize();

        if (!$this->auth->isSuperAdmin()) {
            $this->error('非法操作');
        }

        $this->model = new AuthRule();
            $this->rulelist = $this->model->getRuleMenuList();

    }

    public function table_init()
    {
        Table::$title = '权限管理';
        Table::$page_size = 100;
        //右侧按钮
        Table::$custom_button[] = $this->custom_button[] = Brand::Builder()->build_button('子权限','el-icon-add')
            ->button_url(url('')->build())
            ->button_data(['id'])
            ->button_visible('ismenu')
            ->build();

    }


    public function form_init()
    {
        $pid_select = [
            ['label' => '无','value' => 0]
        ];
        foreach ($this->rulelist as $item) {
            if ($item['ismenu'] != 1) continue;
            $pid_select[] = ['label' => $item['title'],'value' => $item['id']];
        }
        Columns::setForm('pid', 'options' ,$pid_select);

    }

    public function table_search_raws()
    {
        Form::$searchRaw = ['pid' => $this->request->param('id',"")];
    }

    public function _tableData()
    {
        $pid = $this->request->param('pid');
        if ($pid){
            $list = $this->model->where(['pid'=>$pid])->select();
        }else{
            $list = $this->rulelist;
        }
        return _success('',['count'=>count($list),'list'=>$list]);
    }











}
